package com.dongjiantong.web.protal.controller.quarantine;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.biz.enums.ReportType;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.web.sso.LoginInfo;
import com.dongjiantong.common.web.util.UserUtil;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.quarantine.dto.SupervisionRecordDto;
import com.dongjiantong.quarantine.enums.FileType;
import com.dongjiantong.quarantine.model.SupervisionRecords;
import com.dongjiantong.quarantine.serviceImpl.BreedReportsFileReadService;
import com.dongjiantong.quarantine.serviceImpl.SupervisionRecordsReadService;
import com.dongjiantong.quarantine.serviceImpl.SupervisionRecordsSubReadService;
import com.dongjiantong.quarantine.serviceImpl.SupervisionRecordsWriteService;
import com.dongjiantong.user.model.Farms;
import com.dongjiantong.user.service.FarmsService;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by mz on 10/1/18.
 *
 */
@Slf4j
@OpenBean
public class SupervisionRecordsWebController {

    @Reference(version = "1.0.0")
    private SupervisionRecordsReadService supervisionRecordsReadService;
    @Reference(version = "1.0.0")
    private SupervisionRecordsWriteService supervisionRecordsWriteService;
    @Reference(version = "1.0.0")
    private SupervisionRecordsSubReadService supervisionRecordsSubReadService;
    @Reference(version = "1.0.0")
    private BreedReportsFileReadService breedReportsFileReadService;
    @Reference(version = "1.0.0")
    private FarmsService farmsService;

    @OpenMethod(
            key = "web.paging.supervision.record.for.gov.list",
            paramNames = {"pageNum", "pageSize", "farmId", "status","areaList","breedType","userId","startDate","endDate"},
            httpMethods = RequestMethod.GET
    )
    public Response<PageInfo<SupervisionRecords>> pagingForWebGovList(Integer pageNum,
                                                                      Integer pageSize,
                                                                      String farmId,
                                                                      Integer status,
                                                                      List<Integer> areaList,
                                                                      List<Integer> breedType,
                                                                      String userId,
                                                                      String startDate,
                                                                      String endDate) {
        try {
            Map<String, Object> map = Maps.newHashMap();
            List<Long> farmIds = new ArrayList<>();
            farmIds.add(-1L);
            if (!Strings.isNullOrEmpty(farmId)){
                farmIds.add(Long.parseLong(farmId));
            }else {
                LoginInfo loginInfo = UserUtil.getCurrentUser();
                if(areaList == null || areaList.isEmpty()){
                    List<Farms> farmsList = farmsService.selectByUserId(loginInfo.getId());
                    for(Farms farms : farmsList) farmIds.add(farms.getId());
                }else {
                    Map<String , Object> map1 = Maps.newHashMap();
                    int i = 0;
                    for (Integer areaCode : areaList) {
                        if (i == 0) {
                            map1.put("provinceId",areaCode);
                        } else if (i == 1) {
                            map1.put("cityId",areaCode);
                        } else if (i == 2) {
                            map1.put("regionId",areaCode);
                        } else if (i == 3) {
                            map1.put("streetId",areaCode);
                        } else if (i == 4) {
                            map1.put("villageId",areaCode);
                        }
                        i++;
                    }
                    map1.put("userId",loginInfo.getId());
                    List<Farms> farmsList = farmsService.selectByMap(map1);
                    for(Farms farms : farmsList) farmIds.add(farms.getId());
                }

            }
            map.put("farmIds", farmIds);
            if (status != null) map.put("status", status);
            if (breedType.size() > 0){
                switch (breedType.size()){
                    case 1:
                        map.put("breedType",breedType.get(0));
                        break;
                    case 2:
                        map.put("breedSecondType",breedType.get(1));
                        break;
                    case 3:
                        map.put("breedThirdType",breedType.get(2));
                    default:
                        break;
                }
            }
            if (!Strings.isNullOrEmpty(startDate)){
                Date startDate1 = new Date(Long.valueOf(startDate));
                map.put("start",startDate1);
            }
            if (!Strings.isNullOrEmpty(endDate)){
                Date endDate1 = new Date(Long.valueOf(endDate) + 86400000);
                map.put("end",endDate1);
            }
            if(!Strings.isNullOrEmpty(userId)){
                map.put("userId",Long.valueOf(userId));
            }
            map.put("deleteFlag",1);

            PageInfo<SupervisionRecords> supervisionRecordsPageInfo = supervisionRecordsReadService.pagingForGovList(pageNum, pageSize, map);
            for(SupervisionRecords supervisionRecords : supervisionRecordsPageInfo.getList()){
                map.put("field", "photo_supervision_list");
                map.put("type1", ReportType.SUPERVISION_RECORD.value());
                map.put("type2", FileType.PHOTO.value());
                map.put("relevanceId", supervisionRecords.getId());
                List<String> photo_supervision_list = breedReportsFileReadService.findListByType(map);
                map.put("field", "video_supervision_list");
                map.put("type2", FileType.VIDEO.value());
                List<String> video_supervision_list = breedReportsFileReadService.findListByType(map);
                map.put("field", "video_photo_supervision_list");
                map.put("type2", FileType.PHOTO.value());
                List<String> video_photo_supervision_list = breedReportsFileReadService.findListByType(map);
                supervisionRecords.setPhoto_supervision_list(photo_supervision_list);
                supervisionRecords.setVideo_supervision_list(video_supervision_list);
                supervisionRecords.setVideo_photo_supervision_list(video_photo_supervision_list);
            }
            return Response.ok(supervisionRecordsPageInfo);
        } catch (Exception e) {
            return Response.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMessage());
        }
    }

    @OpenMethod(
            key = "web.get.supervision.record.users",
            httpMethods = RequestMethod.GET
    )
    public Response<List<Map<String , Object>>> getUser(){
        try{
            Map<String , Object> map = Maps.newHashMap();
            List<Long> farmIds = new ArrayList<>();
            farmIds.add(-1L);
            LoginInfo loginInfo = UserUtil.getCurrentUser();
            List<Farms> farmsList = farmsService.selectByUserId(loginInfo.getId());
            for(Farms farms : farmsList) farmIds.add(farms.getId());
            map.put("farmIds",farmIds);
            return Response.ok(supervisionRecordsReadService.findUser(map));
        }catch (Exception e){
            return Response.fail(HttpCode.INTERNAL_SERVER_ERROR.getMessage());
        }
    }


    @OpenMethod(
            key = "web.paging.supervision.record.for.map",
            paramNames = {"farmId","areaList","breedType","userId","startDate","endDate"},
            httpMethods = RequestMethod.GET
    )
    public Response<List<SupervisionRecordDto>> getForWebMap(String farmId,
                                                             List<Integer> areaList,
                                                             List<Integer> breedType,
                                                             String userId,
                                                             String startDate,
                                                             String endDate){

        try{
            Map<String, Object> map = Maps.newHashMap();
            List<Long> farmIds = new ArrayList<>();
            farmIds.add(-1L);
            if (!Strings.isNullOrEmpty(farmId)){
                farmIds.add(Long.parseLong(farmId));
            }else {
                LoginInfo loginInfo = UserUtil.getCurrentUser();
                if(areaList == null || areaList.isEmpty()){
                    List<Farms> farmsList = farmsService.selectByUserId(loginInfo.getId());
                    for(Farms farms : farmsList) farmIds.add(farms.getId());
                }else {
                    Map<String , Object> map1 = Maps.newHashMap();
                    int i = 0;
                    for (Integer areaCode : areaList) {
                        if (i == 0) {
                            map1.put("provinceId",areaCode);
                        } else if (i == 1) {
                            map1.put("cityId",areaCode);
                        } else if (i == 2) {
                            map1.put("regionId",areaCode);
                        } else if (i == 3) {
                            map1.put("streetId",areaCode);
                        } else if (i == 4) {
                            map1.put("villageId",areaCode);
                        }
                        i++;
                    }
                    map1.put("userId",loginInfo.getId());
                    List<Farms> farmsList = farmsService.selectByMap(map1);
                    for(Farms farms : farmsList) farmIds.add(farms.getId());
                }

            }
            if (breedType.size() > 0){
                switch (breedType.size()){
                    case 1:
                        map.put("breedType",breedType.get(0));
                        break;
                    case 2:
                        map.put("breedSecondType",breedType.get(1));
                        break;
                    case 3:
                        map.put("breedThirdType",breedType.get(2));
                    default:
                        break;
                }
            }
            Long oneDay = 86400000L;
            Date start = !Strings.isNullOrEmpty(startDate) ? new Date(Long.valueOf(startDate)) : null;
            Date end = !Strings.isNullOrEmpty(endDate) ? new Date(Long.valueOf(endDate) + oneDay) : null;
            map.put("farmIds", farmIds);
            map.put("deleteFlag",1);
            if(!Strings.isNullOrEmpty(userId)){
                map.put("userId",Long.valueOf(userId));
            }
            List<SupervisionRecords> supervisionRecordsList = supervisionRecordsReadService.findListForGovMap(map);
            List<SupervisionRecordDto> supervisionRecordDtoList = new ArrayList<>();
            for (SupervisionRecords supervisionRecords : supervisionRecordsList) {
                SupervisionRecordDto supervisionRecordsDto = new SupervisionRecordDto();
                supervisionRecordsDto.setSupervisionRecords(supervisionRecords);
                Long farm_id = supervisionRecords.getFarm_id();
                map.put("farmId", farm_id);
                List<SupervisionRecords> supervisionRecordsList1 = supervisionRecordsReadService.findListForGovMapByFarmId(map);
                supervisionRecordsDto.setArrayList(supervisionRecordsList1);
                supervisionRecordDtoList.add(supervisionRecordsDto);
            }
            return Response.ok(supervisionRecordDtoList);
        }catch (Exception e){
            return Response.fail(e.getMessage());
        }

    }

    @OpenMethod(
            key = "web.delete.supervision.record.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.POST
    )
    public Response<Boolean> deleteSupervisionRecordById(Long id){
        try{
            supervisionRecordsWriteService.updateDeleteFlag(id);
            return Response.ok(Boolean.TRUE);
        }catch (Exception e){
            return Response.fail(e.getMessage());
        }
    }
}
